<template>
  <!--项目总结材料  -->
  <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="1"
      :formType="value.type"
      :labelWidth="150"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
  </div>
</template>

<script>
import { formatdateByDay, formatdateByTime } from '@/utils/index';
import { dictionary } from '@/utils/dictMixin';
export default {
  mixins: [dictionary],
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      formValue: {}
    };
  },
  mounted() {
    console.log(this.value); //获取到行id 、类型（新增、详情）
    //请求接口进行赋值，自己写吧
  },
  methods: {
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    init() {
      if (this.value.type === 'edit') {
        this.initForm.forEach(x => {
          x.initialValue = this.value.row[x.fieldName];
          if (x.fieldName == 'realLaunchReleaseTimeList') {
            //如果为实际开展/投放时间
            if (this.value.row['realLaunchReleaseTime'] != null && this.value.row['realLaunchReleaseTime'] != '') {
              let releaseTime = this.value.row['realLaunchReleaseTime'].split(',');
              x.initialValue = releaseTime;
            }
          }
        });
      }
    },
    //搜索数据
    initFormFun() {
      return [
          {
          type: 'UPLOAD_FILE',
          label: '现场高清照片',
          disabled: this.type === 'show' ? true : false,
          fieldName: 'photoUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload'
          }
        },
        {
          type: 'UPLOAD_FILE',
          label: '项目核销材料',
          disabled: this.type === 'show' ? true : false,
          fieldName: 'cancellationUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload'
          }
        },
        {
          type: 'UPLOAD_FILE',
          label: '实际使用费用明细',
          disabled: this.type === 'show' ? true : false,
          fieldName: 'feeDetailUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload'
          }
        },
        {
          type: 'UPLOAD_FILE',
          label: '合同报价单',
          disabled: this.type === 'show' ? true : false,
          fieldName: 'contractOfferUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload'
          }
          // rules: [{ required: true, message: '请上传合同报价单', trigger: 'blur' }]
        },
        {
          type: 'UPLOAD_FILE',
          label: '发票',
          disabled: this.type === 'show' ? true : false,
          fieldName: 'billUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload'
          }
          // rules: [{ required: true, message: '请上传发票', trigger: 'blur' }]
        },
        {
          type: 'UPLOAD_FILE',
          label: '其它',
          fieldName: 'otherUrl',
          initialValue: '',
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 99
          }
        } 
      ];
    },
     //实际开展/投放时间 改变方法
    initReleaseTimeSmall(val) {
      //清空 计划支持场次/投放天数、开始日期、结束日期

      this.initForm.find(item => item.fieldName === 'realTimeDay').initialValue = 0;
      this.initForm.find(item => item.fieldName === 'realStartDate').initialValue = '';
      this.initForm.find(item => item.fieldName === 'realEndDate').initialValue = '';
      // this.initForm.find(item => item.fieldName === 'projectFile').initialValue = vlue
      if (val != null && val.length > 0) {
        //如果计划开展/投放时间不为空
        //设置计划支持场次/投放天数为所选天的个数
        this.initForm.find(item => item.fieldName === 'realTimeDay').initialValue = val.length;
        let realStartDateTmp = '';
        let realEndDateTmp = '';
        //冒泡获取开始日期和结束日期
        for (let i = 0; i < val.length; i++) {
          if (!realStartDateTmp) {
            realStartDateTmp = val[i];
            realEndDateTmp = val[i];
          } else {
            if (realStartDateTmp > val[i]) {
              realStartDateTmp = val[i];
            }
            if (realEndDateTmp < val[i]) {
              realEndDateTmp = val[i];
            }
          }
        }
        this.initForm.find(item => item.fieldName === 'realStartDate').initialValue = formatdateByTime(realStartDateTmp);
        this.initForm.find(item => item.fieldName === 'realEndDate').initialValue = formatdateByTime(realEndDateTmp);
      }
    },
       //订单完成率
    orderCalcChange(val) {
      let orderCompletionRate = ((val * 100) / this.value.row.targetOrderNum).toFixed(2);
      if (!isNaN(orderCompletionRate)) {
        this.initForm.find(item => item.fieldName === 'orderCompletionRate').initialValue = parseFloat(orderCompletionRate);
      }
    },
    formChangeHandle(val) {
      this.formValue = val;
      // //传递数据到父组件里
      //this.$emit('click', this.formValue);
      // //触发外部的事件，以标志着本表单已经准备完毕
      this.$emit('formchange', this.formValue);
    }
  },
  created() {
    this.init();
  }
};
</script>
